package com.atguigu.boot.admin.controller;

import com.atguigu.boot.admin.bean.User;
import com.atguigu.boot.admin.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author : leimingchao
 * @date Date : 2021年11月19日 21:43
 */
@Controller
public class TableController {

    @Autowired
    private UserService userService;

    /**
     * 不带请求参数或者参数类型不对 400：Bad Request 一般都是浏览器传的参数不对
     * @param a
     * @return
     */
    @GetMapping(value = "/basic_table")
    public String basic_table(@RequestParam(value = "a") int a) {
        int i = 10 / 0;
        return "table/basic_table";
    }

    @GetMapping(value = "/user/delete/{id}")
    public String deleteUser(@PathVariable(value = "id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1") Long pn,
                             RedirectAttributes redirectAttributes) {
        userService.removeById(id);
        redirectAttributes.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @GetMapping(value = "/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1") Long pn,Model model) {
        //表格内容的遍历
        //response.sendError
        // List<User> users = Arrays.asList(new User("zhangsan", "123456"),
        //         new User("lisi", "123444"),
        //         new User("haha", "aaaaaa"),
        //         new User("hehe", "aaddd"));
        // model.addAttribute("users",users);
        // if (users.size() > 3) {
        //     throw new UserTooManyException();
        // }

        //从数据库中查出user表中的用户进行展示
        List<User> users = userService.list();
        // model.addAttribute("users",users);

        //分页查询数据
        Page userPage = new Page(pn,2);
        Page page = userService.page(userPage, null);
        model.addAttribute("users",page);

        return "table/dynamic_table";
    }

    @GetMapping(value = "/responsive_table")
    public String responsive_table() {

        return "table/responsive_table";
    }

    @GetMapping(value = "/editable_table")
    public String editable_table() {

        return "table/editable_table";
    }
}
